package com.mz.ozy.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.mz.ozy.R;

public class CommonDialog extends Dialog implements View.OnClickListener {
    private int width = 280;
    private int height = 160;
    private TextView contentTxt;
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;
    private int layoutid = 0;

    private Context mContext;
    private String content;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;

    public CommonDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public CommonDialog(Context context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
    }

    public CommonDialog(Context context, int themeResId, String content, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
    }

    public CommonDialog(Context context, int themeResId, int layoutid, String content, OnCloseListener listener, int width, int height) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
        this.layoutid = layoutid;
        this.width = width;
        this.height = height;
    }

    protected CommonDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public CommonDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public CommonDialog setPositiveButton(String name) {
        this.positiveName = name;
        return this;
    }

    public CommonDialog setNegativeButton(String name) {
        this.negativeName = name;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (layoutid == 0) {
            setContentView(R.layout.dialog_commom);
        } else {
            setContentView(layoutid);
        }
        setCanceledOnTouchOutside(true);

        /*Window window = getWindow();
        if (window != null) {
            // 一定要设置Background，如果不设置，window属性设置无效
//            window.setBackgroundDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.white)));
            DisplayMetrics dm = new DisplayMetrics();
            if (((Activity) mContext) != null) {
                WindowManager windowManager = ((Activity) mContext).getWindowManager();
                if (windowManager != null) {
                    windowManager.getDefaultDisplay().getMetrics(dm);
                    WindowManager.LayoutParams params = window.getAttributes();
                    params.gravity = Gravity.CENTER;
                    // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
//                    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
//                    params.height = height;
                    params.width = ScreenUtil.dip2px(mContext, width); //设置宽度
                    params.height = ScreenUtil.dip2px(mContext, height);
//                            dm.heightPixels; //设置高度
                    window.setAttributes(params);
                }
            }
        }*/
        initView();
    }

    private void initView() {
        contentTxt = (TextView) findViewById(R.id.content);
        titleTxt = (TextView) findViewById(R.id.title);
        submitTxt = (TextView) findViewById(R.id.submit);
        submitTxt.setOnClickListener(this);
        cancelTxt = (TextView) findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);

        contentTxt.setText(content);
        if (!TextUtils.isEmpty(positiveName)) {
            submitTxt.setText(positiveName);
        }

        if (!TextUtils.isEmpty(negativeName)) {
            cancelTxt.setText(negativeName);
        }

        if (!TextUtils.isEmpty(title)) {
            titleTxt.setText(title);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                if (listener != null) {
                    listener.onClick(false);
                }
                this.dismiss();
                break;
            case R.id.submit:
                if (listener != null) {
                    listener.onClick(true);
                }
                this.dismiss();
                break;
        }
    }

    public interface OnCloseListener {
        void onClick(boolean confirm);
    }
}